'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'shiftform'], function($) {
  return {
    init : function(){
        
     this.edit();
     this.editDescription();
        
    },
    
    edit : function() {
        $(document).on('shown.bs.modal', '#coupon-template-edit', function(e){
            
            var selectedType = $('select[name="type"]').find("option:selected").val();
            
            if(selectedType == 'PRODUCT') {
              $(".product").removeClass('hide');
              $(".productType").addClass('hide');
              $('select[name="product"]').data("validate", "required");
              $('select[name="productType"]').data("validate", "");
            }else if(selectedType == "CATEGORY"){
              $(".productType").removeClass('hide');
              $(".product").addClass('hide');
              $('select[name="product"]').data("validate", "");
              $('select[name="productType"]').data("validate", "required");
            }else {
              $(".productType").addClass('hide');
              $(".product").addClass('hide');
              $('select[name="product"]').data("validate", "");
              $('select[name="productType"]').data("validate", "");
            }
            
            $('select[name="type"]').change(function(e){
              if(e.val == "PRODUCT") {
                $(".product").removeClass('hide');
                $(".productType").addClass('hide');
                $('select[name="product"]').data("validate", "required");
                $('select[name="productType"]').data("validate", "");
                $('select[name="productType"]').val("");
              }else if(e.val == "CATEGORY"){
                $(".productType").removeClass('hide');
                $(".product").addClass('hide');
                $('select[name="product"]').data("validate", "");
                $('select[name="product"]').val("");
                $('select[name="productType"]').data("validate", "required");
              }else {
                $(".productType").addClass('hide');
                $(".product").addClass('hide');
                $('select[name="product"]').data("validate", "");
                $('select[name="productType"]').data("validate", "");
                $('select[name="product"]').val("");
                $('select[name="productType"]').val("");
              }
            });
            
            var selectedType = $('select[name="discountType"]').find("option:selected").val();
            
            if(selectedType == 'FIXED_AMOUNT') {
              $("#fixed_amount").removeClass('hide');
              $("#fixed_amount").find("input").attr("data-validate", "required|float");
            }else if(selectedType == "PERCENT"){
              $("#fixed_amount").addClass('hide');
              $("#fixed_amount").find("input").removeAttr("data-validate");
            }else {
              $("#fixed_amount").addClass('hide');
              $("#fixed_amount").find("input").removeAttr("data-validate");
            }
            
            $('select[name="discountType"]').change(function(e){
              if(e.val == "FIXED_AMOUNT") {
            	  $("#fixed_amount").removeClass('hide');
                  $("#fixed_amount").find("input").attr("data-validate", "required|float");
              }else if(e.val == "PERCENT"){
                $("#fixed_amount").addClass('hide');
                $("#fixed_amount").find("input").removeAttr("data-validate");
                $("#fixed_amount").find("input").val("");
              }else {
                $("#fixed_amount").addClass('hide');
                $("#fixed_amount").find("input").removeAttr("data-validate");
                $("#fixed_amount").find("input").val("");
              }
            });
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
          })
      },
      
      editDescription: function() {
        $(document).on('shown.bs.modal', '#coupon-template-edit-description', function(e){
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      }
    
  }
})

